/**********************************************************************
gc_11_structural_Xpost.do

**********************************************************************/
**********
* SET UP *
**********
clear all
set matsize 2000
set more 1

* location for dofiles *
cd "T:\_Projet_4915\dofiles"
global dir "`c(pwd)'"
cd $dir

set seed 8397740

sysdir set PERSONAL "T:\_Projet_4915\ado"

* specify file locations *
global project_folder "\_Projet_4915"
global data_folder "\_Projet_4915\DATA"
global output_folder "\_Projet_4915\ResultsFolder"
global temp "temp"

local datadir T:\${data_folder}\

*********
* GATES *
*********
* Specify which data you want to work with (synthetic = syn, real = rl) *
local ext = "rl"

* Specify which gender to process (men = men) *
local gender = "men"

* Specify first observed age (24 25 26) *
local a = 25

* Specify occupational definition to use (occ_version 1 2)
local v = 2

* Specify firm class definition (1 2 3 4 5 6)
local d = 6

* Specify the number of unobservable types *
global types  "5"

* Specify earnings data generating process *
local y_dgp = 3

* STEP 1: create a validation dataset *
local gate1 = 1

* STEP 2: external fit, 2012 data *
local gate2 = 1

* STEP 3: choices by type, data and model (plus baseline characteristics)  *
local gate3 = 1

* STEP 5:industry breakdown by type, data  *
local gate5 = 1

* STEP X: delete all intermediate datasets *
local gateX = 1

*******************************************
* MAKE A GLOBAL VARIABLE FOR TODAY'S DATE *
*******************************************
local tyr = substr("$S_DATE",8,4)
local tmo = substr("$S_DATE",4,3)
local tmd = trim(substr("$S_DATE",1,2))

* make day of month two digits *
local wl = length("`tmd'")
if `wl'==1 {
local tmd2 ="0"+"`tmd'"
}
if `wl'!=1 {
local tmd2 ="`tmd'"
}

* get numeric month, make it two digits *
local di="`tmd2'"+"`tmo'"+"`tyr'"
local edate = date("`di'", "DMY")
local mono = month(`edate')
local ml = length("`mono'")
if `ml'==1 {
local mono2 ="0"+"`mono'"
}
if `ml'!=1 {
local mono2 ="`mono'"
}

* put final date together *
global date = "`tyr'"+"`mono2'"+"`tmd2'"

****************************************
* DEFINE LITTLE PROGRAMS TO PRINT TIME *
****************************************
program define starttime
	display "Started processing at $S_TIME on $S_DATE"
end

program define endtime
	display "Finished processing at $S_TIME on $S_DATE"
end


************************
* START OF THE PROGRAM *
************************
foreach n_types of global types ///
{
* start log file *
quietly capture log close
quietly log using gc_11_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'_`ext', text replace

***************************************
* STEP 1: construct simulated dataset *
***************************************
disp "***** Started processing STEP 1 *****"
starttime
if 1 == `gate1' ///
{
	disp "***** STEP 1: construct simulated dataset *****"
	disp "***** Time is now $S_TIME *****"
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_analyze_me_tmp_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	keep if age == 0
	rename state state_index
	merge m:1 pid_int state_index using "`datadir'gc_structural_Qs_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	rename state_index state
	reshape wide q, i(pid_int) j(state)
	keep pid_int q*
	order pid_int q*
	
	**************************************
	* STEP 1.1: DRAW UNOBSERVABLE TYPE *
	**************************************
	if `n_types' == 3 ///
	{
		* Pile up posterior probabilities into a sort of cdf *
		gen a = q1
		gen b = a + q2
		gen c = b + q3
	
		* Draw uniform (0,1) *
		gen draw = runiform()
	
		* Draw unobservable type *
		gen state = 0
		replace state = 1 if draw < a
		replace state = 2 if draw >= a & draw < b
		replace state = 3 if draw >= b & draw <= c
	}

	if `n_types' == 4 ///
	{
		* Pile up posterior probabilities into a sort of cdf *
		gen a = q1
		gen b = a + q2
		gen c = b + q3
		gen d = c + q4
	
		* Draw uniform (0,1) *
		gen draw = runiform()
	
		* Draw unobservable type *
		gen state = 0
		replace state = 1 if draw < a
		replace state = 2 if draw >= a & draw < b
		replace state = 3 if draw >= b & draw < c
		replace state = 4 if draw >= c & draw <= d
	}
	
	if `n_types' == 5 ///
	{
		* Pile up posterior probabilities into a sort of cdf *
		gen a = q1
		gen b = a + q2
		gen c = b + q3
		gen d = c + q4
		gen e = d + q5
	
		* Draw uniform (0,1) *
		gen draw = runiform()
	
		* Draw unobservable type *
		gen state = 0
		replace state = 1 if draw < a
		replace state = 2 if draw >= a & draw < b
		replace state = 3 if draw >= b & draw < c
		replace state = 4 if draw >= c & draw < d
		replace state = 5 if draw >= d & draw <= e
	}
	
	if `n_types' == 6 ///
	{
		* Pile up posterior probabilities into a sort of cdf *
		gen a = q1
		gen b = a + q2
		gen c = b + q3
		gen d = c + q4
		gen e = d + q5
		gen f = e + q6
	
		* Draw uniform (0,1) *
		gen draw = runiform()
	
		* Draw unobservable type *
		gen state = 0
		replace state = 1 if draw < a
		replace state = 2 if draw >= a & draw < b
		replace state = 3 if draw >= b & draw < c
		replace state = 4 if draw >= c & draw < d
		replace state = 5 if draw >= d & draw < e
		replace state = 6 if draw >= e & draw <= f
	}
	
	keep pid_int state q*
	
	save "`datadir'gc_temp_Xpost_types.dta", replace
	
	use "`datadir'gc_analyze_me_`gender'_start`a'_occ_v`v'_`ext'.dta", clear

	*****************************************************************
	* construct a two digit categorical variable to denote action *
	*****************************************************************
	* first digit denotes occupation *
	* 1 = paid employee, 2 = self-employed, 3 = entrepreneur, 9 = unemployed *
	tostring(occ_v`v'), gen(first_digit)
	replace first_digit = "9" if occ_v`v' == 0
	
	* second digit denotes firm class *
	gen firm_class = 0
	
	if `d' == 1 ///
	{
		replace firm_class = 1 if f1_employment_4c == 1
		replace firm_class = 2 if f1_employment_4c == 2
		replace firm_class = 3 if f1_employment_4c == 3 | f1_employment_4c == 4
	}
	if `d' == 2 ///
	{
		replace firm_class = 1 if f2_employment_4c == 1
		replace firm_class = 2 if f2_employment_4c == 2
		replace firm_class = 3 if f2_employment_4c == 3 | f2_employment_4c == 4
	}
	if `d' == 3 ///
	{
		replace firm_class = 1 if f1_rev_perL_4c == 1
		replace firm_class = 2 if f1_rev_perL_4c == 2
		replace firm_class = 3 if f1_rev_perL_4c == 3 | f1_rev_perL_4c == 4
	}
	if `d' == 4 ///
	{
		replace firm_class = 1 if f2_rev_perL_4c == 1
		replace firm_class = 2 if f2_rev_perL_4c == 2
		replace firm_class = 3 if f2_rev_perL_4c == 3 | f2_rev_perL_4c == 4
	}
	if `d' == 5 ///
	{
		replace firm_class = 1 if f1_payroll_perL_4c == 1
		replace firm_class = 2 if f1_payroll_perL_4c == 2
		replace firm_class = 3 if f1_payroll_perL_4c == 3 | f1_payroll_perL_4c == 4
	}
	if `d' == 6 ///
	{
		replace firm_class = 1 if f2_payroll_perL_4c == 1
		replace firm_class = 2 if f2_payroll_perL_4c == 2
		replace firm_class = 3 if f2_payroll_perL_4c == 3 | f2_payroll_perL_4c == 4
	}
				
	tostring(firm_class), gen(second_digit)
	replace second_digit = "0" if occ_v`v' == 0
	replace second_digit = "0" if occ_v`v' == 2
	replace second_digit = "0" if occ_v`v' == 3
	gen action = first_digit + second_digit
	destring(action), replace
	drop second_digit
				
	*****************************************************************************
	* create categorical variable to denote action over the past 11 periods *
	*****************************************************************************
	capture drop period
	sort pid tax_yr
	by pid: gen period = _n
	tab period
	forvalues i = 1(1)11 ///
	{
		gen L`i'action = .
		by pid: replace L`i'action = action[_n - `i'] if period > `i'
	}
	
	***********************************
	* construct vectors of experience *
	***********************************
	* create a list of all possible actions *
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		forvalues i = 1(1)11 ///
		{
			gen flag`i' = 0
			replace flag`i' = 1 if L`i'action == `act'
		}
		* construct vector of experience accumulated since first year *
		egen exper_`act' = rowtotal(flag*)
		drop flag*
	}

	* Set last action to L2action if last action is nonemployment and L2action != . *
	rename L1action last_action
	rename L2action lastlast_action
	replace last_action = lastlast_action if last_action == 90 & lastlast_action != .
	drop L* lastlast_action
	
	rename last_action Laction
	
	* construct main outcome variable *
	gen y = y_v`v'
	gen ln_y = log(y)
	
	* construct numeric person ID *
	destring pid, gen(pid_int)

	* Set initial condition for Laction to action in the initial period *
	replace Laction = action if age == `a'
	
	* construct aggregate experience variables *
	replace age = age-`a'
	
	sort pid_int tax_yr
	
	merge m:1 pid_int using "`datadir'gc_temp_Xpost_types.dta"
	keep if _merge == 3
	
	rename action real_action
	rename ln_y real_ln_y
	global keep_vars ""
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		rename  `var' `var'_index
	}

	keep pid_int dob_yr entry_yr exit_yr res_pc_inc_entry res_pc_shr_bus_entry res_pc_popden_entry res_pc_busipopden_entry res_city_size_entry state q* ///
	tax_yr eid_Ind eid_all eid_startup eid_Ind_long eid_Bus_long res_pc res_prov res_cma_name res_cma_code res_city_size_3c res_city_size_4c res_city_size_5c res_city_size_6c ///
	res_pc_inc res_pc_pop res_pc_busipop res_pc_shr_bus naics4_int naics2_int naics3_int ///
	real_ln_y real_action Laction_index exper_11_index exper_12_index exper_13_index exper_20_index exper_30_index age_index
	
	ds
	global keep_vars "`r(varlist)'"
	keep $keep_vars
	order $keep_vars
	compress

	gen V_0 = 1
	
	***************************
	* STEP 1.2: FIX ACTION j' *
	***************************
	* Create synthetic variables *
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* construct synthetic aggregate experience variables *
	gen exper_10 = exper_11 + exper_12 + exper_13	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}		
	
	local counter = 1
	
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}

	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP11
	gen b = a + CCP12
	gen c = b + CCP13
	gen d = c + CCP20
	gen e = d + CCP30
	gen f = e + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action *
	gen zaction_jp = 0
	replace zaction_jp = 11 if draw < a
	replace zaction_jp = 12 if draw >= a & draw < b
	replace zaction_jp = 13 if draw >= b & draw < c
	replace zaction_jp = 20 if draw >= c & draw < d
	replace zaction_jp = 30 if draw >= d & draw < e
	replace zaction_jp = 90 if draw >= e & draw <= 1
	
	drop CCP*
	
	* Drop all synthetic variables *
	keep $keep_vars z* V_*
	
	****************************
	* STEP 1.3: FIX ACTION j'' *
	****************************
	* Create synthetic variables *
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		gen `var' = `var'_index
	}
	
	* Forward simulate synthetic variables *
	replace Laction = zaction_jp
	replace age = age + 1

	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace exper_`act' = exper_`act' + 1 if zaction_jp == `act'
	}
	
	* construct synthetic aggregate experience variables *
	gen exper_10 = exper_11 + exper_12 + exper_13	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Bring CCP matrix using appropriate STER files from first stage *
	
	* Create CCPs *
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		gen CCP`act' = .
	}
	
	local counter = 1
	* Loop through state *
	forvalues s = 1(1)`n_types' ///
	{
	* Loop through Laction *
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		* Loop through action dummies *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
			disp "***** Iteration `counter', time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			estimates clear
			replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
			drop tmp*
			local counter = `counter' + 1
		}
	}
	}
	
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
		replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
	}
	
	gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		replace CCP`act' = CCP`act'/CCP_TOT
	}
	drop CCP_TOT
	
	* Pile up the CCPs into a sort of cdf *
	gen a = CCP11
	gen b = a + CCP12
	gen c = b + CCP13
	gen d = c + CCP20
	gen e = d + CCP30
	gen f = e + CCP90
	
	* Draw uniform (0,1) *
	gen draw = runiform()
	
	* Draw action j'' *
	gen zaction_jpp = 0
	replace zaction_jpp = 11 if draw < a
	replace zaction_jpp = 12 if draw >= a & draw < b
	replace zaction_jpp = 13 if draw >= b & draw < c
	replace zaction_jpp = 20 if draw >= c & draw < d
	replace zaction_jpp = 30 if draw >= d & draw < e
	replace zaction_jpp = 90 if draw >= e & draw <= 1
	drop CCP*
	
	* Drop all synthetic variables *
	keep $keep_vars z* V_*
	
	***************************************************
	* STEP 1.4: FOR EACH j in J CALCULATE OFFSET TERM *
	***************************************************
	foreach j in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		* Pick action j *
		gen zaction_j = `j'
		* Pick action cj *
		gen zaction_cj = zaction_jp
		* Pick action cjp *
		gen zaction_cjp = zaction_j
		* Pick action cjpp *
		gen zaction_cjpp = zaction_jpp
		
		****************************
		* CCPs and income associated with j *
		****************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
	
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_j = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_j = ln_y_hat_`act' if zaction_j == `act'
		}
		replace zln_y_hat_j = ln(10400) if zaction_j == 90
		drop ln_y_hat*
	
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		****************************
		* CCPs and income associated with j'. *
		****************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_j
		replace age = age + 1
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_j == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_jp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_jp = CCP`act' if zaction_jp == `act'
		}
		
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_jp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_jp = ln_y_hat_`act' if zaction_jp == `act'
		}
		replace zln_y_hat_jp = ln(10400) if zaction_jp == 90
		drop ln_y_hat*
		
		keep $keep_vars z* V_*
		
		****************************************
		* CCPs and income associated with j''. *
		****************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_jp
		replace age = age + 2
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_j == `act'
			replace exper_`act' = exper_`act' + 1 if zaction_jp == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_jpp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_jpp = CCP`act' if zaction_jpp == `act'
		}
	
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_jpp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_jpp = ln_y_hat_`act' if zaction_jpp == `act'
		}
		replace zln_y_hat_jpp = ln(10400) if zaction_jpp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
	
		***************************************
		* CCPs and income associated with cj. *
		***************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cj = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_cj = ln_y_hat_`act' if zaction_cj == `act'
		}
		replace zln_y_hat_cj = ln(10400) if zaction_cj == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		****************************************
		* CCPs and income associated with cj'. *
		****************************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_cj
		replace age = age + 1
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_cj == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}		
		
		local counter = 1
		
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_cjp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_cjp = CCP`act' if zaction_cjp == `act'
		}
		
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cjp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_cjp = ln_y_hat_`act' if zaction_cjp == `act'
		}
		replace zln_y_hat_cjp = ln(10400) if zaction_cjp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		******************************
		* CCPs associated with cj''. *
		******************************
		* Create synthetic variables *
		foreach var in ///
		"Laction" "age" /// 
		"exper_11" "exper_12" "exper_13" ///
		"exper_20" "exper_30" ///
		{
			gen `var' = `var'_index
		}
		
		* Forward simulate synthetic variables *
		replace Laction = zaction_cjp
		replace age = age + 2
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace exper_`act' = exper_`act' + 1 if zaction_cj == `act'
			replace exper_`act' = exper_`act' + 1 if zaction_cjp == `act'
		}
		
		* construct synthetic aggregate experience variables *
		gen exper_10 = exper_11 + exper_12 + exper_13	
		gen age_sq = age*age
		
		* construct polynomials of experience *
		foreach var in ///
		"exper_10" "exper_20" "exper_30" ///
		{
			gen `var'_sq = `var'*`var'
		}
		
		* Bring CCP matrix using appropriate STER files from first stage *
		
		* Create CCPs *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			gen CCP`act' = .
		}
		
		local counter = 1
		* Loop through state *
		forvalues s = 1(1)`n_types' ///
		{
		* Loop through Laction *
		foreach Lact in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			* Loop through action dummies *
			foreach act in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				disp "***** Now running linear probability model locally for: CCP action = `act', Laction = `Lact', and type = `s' *****"
				disp "***** Iteration `counter', time is now $S_TIME *****"
				local datadir T:\${data_folder}\gc_ster\
				estimates use "`datadir'gc_structural_CCP`act'_L`Lact'_s`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
				predict tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				estimates clear
				replace CCP`act' = tmpCCP`act'_`Lact'_`s' if Laction == `Lact' & state == `s'
				drop tmp*
				local counter = `counter' + 1
			}
		}
		}
		
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = 0.0000001 if CCP`act' <= 0.0000001
			replace CCP`act' = 0.9999999 if CCP`act' >= 0.9999999 & CCP`act' !=.
		}
		
		gen CCP_TOT = CCP11 + CCP12 + CCP13 + CCP20 + CCP30 + CCP90
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace CCP`act' = CCP`act'/CCP_TOT
		}
		drop CCP_TOT
		
		* Pick the relevant CCPs *
		gen zCCP_cjpp = .
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" "90" ///
		{
			replace zCCP_cjpp = CCP`act' if zaction_cjpp == `act'
		}
	
		drop CCP*
		
		* Bring ln_y_hat matrix using appropriate STER files from first stage *
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			disp "***** Now estimating parameters of the wage equation for action: `act' *****"
			disp "***** Time is now $S_TIME *****"
			local datadir T:\${data_folder}\gc_ster\
			estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
			predict ln_y_hat_`act'
			estimates clear
		}
		
		* Pick the relevant ln_y_hats *
		gen zln_y_hat_cjpp = 0
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			replace zln_y_hat_cjpp = ln_y_hat_`act' if zaction_cjpp == `act'
		}
		replace zln_y_hat_cjpp = ln(10400) if zaction_cjpp == 90
		drop ln_y_hat*
		
		* Drop all synthetic variables *
		keep $keep_vars z* V_*
		
		local beta = .90
		display "`beta'"
		
		*************************
		* Calculate offset term *
		*************************
		gen Offset = `beta'*log(zCCP_jp/zCCP_cjp) + `beta'*`beta'*log(zCCP_jpp/zCCP_cjpp)
		
		********************************************************************
		* Calculate Delta U using estimated parameters of utility function *
		********************************************************************
		gen z_lny = zln_y_hat_j-zln_y_hat_cj + `beta'*(zln_y_hat_jp-zln_y_hat_cjp) + `beta'*`beta'*(zln_y_hat_jpp-zln_y_hat_cjpp)
	
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			gen amen`act'_j = 0
			replace amen`act'_j = 1-`beta' if zaction_j == `act'
			gen amen`act'_jp = 0
			replace amen`act'_jp = -1*(1-`beta') if zaction_jp == `act'
			gen zamen`act' = amen`act'_j + amen`act'_jp
			drop amen*
		}
		
		display "`beta'"
		foreach act in ///
		"11" "12" "13" ///
		"20" "30" ///
		{
			foreach Lact in ///
			"11" "12" "13" ///
			"20" "30" "90" ///
			{
				gen cost_act`Lact'_`act'_z_j = 0
				replace cost_act`Lact'_`act'_z_j = 1 if Laction == `Lact' & zaction_j == `act' & Laction != zaction_j
				gen cost_act`Lact'_`act'_z_jp = 0
				replace cost_act`Lact'_`act'_z_jp = -1 if Laction == `Lact' & zaction_jp == `act' & Laction != zaction_jp
				gen cost_act`Lact'_`act'_j_jp = 0
				replace cost_act`Lact'_`act'_j_jp = `beta' if zaction_j == `Lact' & zaction_jp == `act' & zaction_j != zaction_jp
				gen cost_act`Lact'_`act'_jp_j = 0
				replace cost_act`Lact'_`act'_jp_j = -`beta' if zaction_jp == `Lact' & zaction_j == `act' & zaction_jp != zaction_j
				gen cost_act`Lact'_`act'_jp_jpp = 0
				replace cost_act`Lact'_`act'_jp_jpp = `beta'*`beta' if zaction_jp == `Lact' & zaction_jpp == `act' & zaction_jp != zaction_jpp
				gen cost_act`Lact'_`act'_j_jpp = 0
				replace cost_act`Lact'_`act'_j_jpp = -`beta'*`beta' if zaction_j == `Lact' & zaction_jpp == `act' & zaction_j != zaction_jpp
				gen zcost_act`Lact'_`act' = cost_act`Lact'_`act'_z_j+cost_act`Lact'_`act'_z_jp+cost_act`Lact'_`act'_j_jp+cost_act`Lact'_`act'_jp_j+cost_act`Lact'_`act'_jp_jpp + cost_act`Lact'_`act'_j_jpp
				*gen zcost_act`Lact'_`act'_age = zcost_act`Lact'_`act'*age
				drop cost*
			}
		}

		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_util_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict Delta_U
		estimates clear
	
		* Now calculate v_j *
		gen v_`j' = Delta_U - Offset
	
		* Now calculate e_j *
		gen rgumbel_`j' = -log(-log(runiform()))
			
		* Now calculate V_j *
		gen V_`j' = v_`j' + rgumbel_`j'

		keep $keep_vars zaction_jp zaction_jpp V_*
	}
		
	**********************************************************
	* Now choose action that maximizes V_j, call that action *
	**********************************************************
	gen maxV = max(V_11,V_12,V_13,V_20,V_30,V_90)
	gen action = 0
	foreach act in ///
	"11" "12" "13"  ///
	"20" "30" "90" ///
	{
		replace action = `act' if round(V_`act',.001) == round(maxV,.001)
	}
	keep $keep_vars action
	
	* save cross-section before updating state variables *
	foreach var in ///
	"Laction" "age" /// 
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		rename `var'_index `var' 
	}

	* construct aggregate experience variables *
	gen exper_10 = exper_11 + exper_12 + exper_13	
	gen age_sq = age*age
	
	* construct polynomials of experience *
	foreach var in ///
	"exper_10" "exper_20" "exper_30" ///
	{
		gen `var'_sq = `var'*`var'
	}
	
	* Simulate log earnings using estimated payoff functions *
	* Bring ln_y_hat matrix using appropriate STER files from first stage *
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		disp "***** Now estimating parameters of the wage equation for action: `act' *****"
		disp "***** Time is now $S_TIME *****"
		local datadir T:\${data_folder}\gc_ster\
		estimates use "`datadir'gc_structural_y`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.ster"
		predict ln_y_hat_`act'
		estimates clear
	}
		
	* Pick the relevant ln_y_hat *
	gen ln_y_hat = .
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace ln_y_hat = ln_y_hat_`act' if action == `act'
	}
	drop ln_y_hat_*
	
	local datadir T:\${data_folder}\
	disp "`datadir'"
	compress
	save "`datadir'gc_tmpfit_X_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	
	* Calculate variance of idiosyncratic shocks for payoff functions (to be used below to simulate earnings) *
	local datadir T:\${data_folder}\
	use "`datadir'gc_analyze_me_tmp_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	foreach var in "state" "age" ///
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		rename `var' `var'_index
	}
	merge m:1 pid_int state_index using "`datadir'gc_structural_Qs_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	merge m:1 state_index age_index ///
	exper_11_index exper_12_index exper_13_index ///
	exper_20_index exper_30_index ///
	using "`datadir'gc_structural_ln_y_hats_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	foreach var in "state" "age" ///
	"exper_11" "exper_12" "exper_13" ///
	"exper_20" "exper_30" ///
	{
		rename `var'_index `var'
	}
	
	gen ln_y_hat = .
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		replace ln_y_hat = ln_y_hat_`act' if action == `act'
	}
	sort pid_int tax_yr state
	gen mu = ln_y-ln_y_hat
	sort action
	gen sigma = .
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" ///
	{
		sum mu [aw=q] if action == `act'
		replace sigma = `r(sd)' if action == `act'
	}
	keep action sigma
	duplicates drop
	save "`datadir'gc_structural_sigmas_X_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace

	use "`datadir'gc_tmpfit_X_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	merge m:1 action using "`datadir'gc_structural_sigmas_X_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	drop if _merge == 2
	drop _merge
	gen shock = rnormal(0,sigma)
	gen ln_y = ln_y_hat+shock
	compress
	save "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace
	clear
	
	capture erase "`datadir'gc_structural_sigmas_X_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	capture erase "`datadir'gc_tmpfit_X_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta"
	capture erase "`datadir'gc_temp_Xpost_types.dta"
	
	disp "***** STEP 1: create a validation dataset (COMPLETED) *****"
}
disp "***** Finished processing STEP 1 *****"
endtime

***************
* STEP 2: fit *
***************
disp "***** Started processing STEP 2 *****"
starttime
if 1 == `gate2' ///
{
	disp "***** STEP 2: fit *****"
	
	************************************************************
	* Row transition matrix age 30, in-sample one period ahead *
	************************************************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr	
	replace age = age+25
	*drop if tax_yr == 2012
	keep if age == 30

	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	
	*** calculate the share of action by last action ***
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if Laction == `Lact'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q] 
		gen Laction = "`Lact'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`Lact'" == "11" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`Lact'" != "11" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id 
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q] 
	gen Laction = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if Laction == `Lact'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q] 
		gen Laction = "`Lact'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q] 
	gen Laction = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order Laction
	sort Laction
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order Laction
	sort Laction
	forvalues j = 1(1)6 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 Laction using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	gen sort1 = 0
	replace sort1 = 1 if Laction == "11"
	replace sort1 = 2 if Laction == "12"
	replace sort1 = 3 if Laction == "13"
	replace sort1 = 5 if Laction == "20"
	replace sort1 = 6 if Laction == "30"
	replace sort1 = 7 if Laction == "90"
	sort sort1
	drop sort1
	order Laction obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd ///
	action_shr5 action_shr5_sd ///
	action_shr6 action_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_rowtransit_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear

	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	*************************
	* Row transition matrix, out of sample predicted *
	*************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr	
	replace age = age+25
	keep if tax_yr == 2012
	
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	
	*** calculate the share of action by last action ***
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if Laction == `Lact'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q] 
		gen Laction = "`Lact'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`Lact'" == "11" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`Lact'" != "11" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id 
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q] 
	gen Laction = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if Laction == `Lact'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q] 
		gen Laction = "`Lact'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q] 
	gen Laction = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order Laction
	sort Laction
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order Laction
	sort Laction
	forvalues j = 1(1)6 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 Laction using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	gen sort1 = 0
	replace sort1 = 1 if Laction == "11"
	replace sort1 = 2 if Laction == "12"
	replace sort1 = 3 if Laction == "13"
	replace sort1 = 5 if Laction == "20"
	replace sort1 = 6 if Laction == "30"
	replace sort1 = 7 if Laction == "90"
	sort sort1
	drop sort1
	order Laction obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd ///
	action_shr5 action_shr5_sd ///
	action_shr6 action_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_rowtransit_2012_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear

	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"

	************************************
	* Row transition matrix, 2012 data *
	************************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr	
	replace age = age+25
	keep if tax_yr == 2012
	drop action
	rename real_action action
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	
	*** calculate the share of action by last action ***
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if Laction == `Lact'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		local datadir T:\${data_folder}\gc_esamples\
		saveold "`datadir'gc_Xp_row2012_L`Lact'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
		sum action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
		local datadir T:\${data_folder}\
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int 
		gen Laction = "`Lact'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`Lact'" == "11" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`Lact'" != "11" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	local datadir T:\${data_folder}\gc_esamples\
	saveold "`datadir'gc_Xp_row2012_Lall_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
	sum action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
	local datadir T:\${data_folder}\	
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int
	gen Laction = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	foreach Lact in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if Laction == `Lact'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
		gen Laction = "`Lact'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
	gen Laction = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order Laction
	sort Laction
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order Laction
	sort Laction
	forvalues j = 1(1)6 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 Laction using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	gen sort1 = 0
	replace sort1 = 1 if Laction == "11"
	replace sort1 = 2 if Laction == "12"
	replace sort1 = 3 if Laction == "13"
	replace sort1 = 5 if Laction == "20"
	replace sort1 = 6 if Laction == "30"
	replace sort1 = 7 if Laction == "90"
	sort sort1
	drop sort1
	order Laction obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd ///
	action_shr5 action_shr5_sd ///
	action_shr6 action_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_rowtransit_2012rl_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear

	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	****************************
	* Column transition matrix age 30, in sample one period ahead only *
	****************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr	
	replace age = age+25
	*drop if tax_yr == 2012
	keep if age == 30
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab Laction, gen(Laction_shr)
	
	*** calculate the share of action by action ***
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if action == `act'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6 (count) ind=n_ind obs=pid_int [aw=q]
		gen action = "`act'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`act'" == "11" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`act'" != "11" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	collapse (mean) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6 (count) ind=n_ind obs=pid_int [aw=q]
	gen action = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if action == `act'
		collapse (sd) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6 [aw=q]
		gen action = "`act'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6 [aw=q]
	gen action = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order action
	sort action
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order action
	sort action
	forvalues j = 1(1)6 ///
	{
		rename Laction_shr`j' Laction_shr`j'_sd
	}
	merge 1:1 action using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	gen sort1 = 0
	replace sort1 = 1 if action == "11"
	replace sort1 = 2 if action == "12"
	replace sort1 = 3 if action == "13"
	replace sort1 = 5 if action == "20"
	replace sort1 = 6 if action == "30"
	replace sort1 = 7 if action == "90"
	sort sort1
	drop sort1
	order action obs ind ///
	Laction_shr1 Laction_shr1_sd ///
	Laction_shr2 Laction_shr2_sd ///
	Laction_shr3 Laction_shr3_sd ///
	Laction_shr4 Laction_shr4_sd ///
	Laction_shr5 Laction_shr5_sd ///
	Laction_shr6 Laction_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_coltransit_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear

	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	****************************
	* Column transition matrix, out of sample 2012 predicted *
	****************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr	
	replace age = age+25
	keep if tax_yr == 2012
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab Laction, gen(Laction_shr)
	
	*** calculate the share of action by action ***
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if action == `act'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6 (count) ind=n_ind obs=pid_int [aw=q]
		gen action = "`act'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`act'" == "11" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`act'" != "11" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	collapse (mean) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6 (count) ind=n_ind obs=pid_int [aw=q]
	gen action = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if action == `act'
		collapse (sd) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6 [aw=q]
		gen action = "`act'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6 [aw=q]
	gen action = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order action
	sort action
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order action
	sort action
	forvalues j = 1(1)6 ///
	{
		rename Laction_shr`j' Laction_shr`j'_sd
	}
	merge 1:1 action using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	gen sort1 = 0
	replace sort1 = 1 if action == "11"
	replace sort1 = 2 if action == "12"
	replace sort1 = 3 if action == "13"
	replace sort1 = 5 if action == "20"
	replace sort1 = 6 if action == "30"
	replace sort1 = 7 if action == "90"
	sort sort1
	drop sort1
	order action obs ind ///
	Laction_shr1 Laction_shr1_sd ///
	Laction_shr2 Laction_shr2_sd ///
	Laction_shr3 Laction_shr3_sd ///
	Laction_shr4 Laction_shr4_sd ///
	Laction_shr5 Laction_shr5_sd ///
	Laction_shr6 Laction_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_coltransit_2012_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear

	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	***************************************
	* Column transition matrix, 2012 data *
	***************************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr	
	replace age = age+25
	keep if tax_yr == 2012
	drop action
	rename real_action action

	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab Laction, gen(Laction_shr)
	
	*** calculate the share of action by action ***
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if action == `act'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		local datadir T:\${data_folder}\gc_esamples\
		saveold "`datadir'gc_Xp_col2012_A`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
		sum Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6
		local datadir T:\${data_folder}\
		collapse (mean) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6 (count) ind=n_ind obs=pid_int
		gen action = "`act'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`act'" == "11" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`act'" != "11" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	local datadir T:\${data_folder}\gc_esamples\
	saveold "`datadir'gc_Xp_col2012_Aall_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
	sum Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6
	local datadir T:\${data_folder}\
	collapse (mean) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6 (count) ind=n_ind obs=pid_int
	gen action = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	foreach act in ///
	"11" "12" "13" ///
	"20" "30" "90" ///
	{
		preserve
		keep if action == `act'
		collapse (sd) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6
		gen action = "`act'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) Laction_shr1 Laction_shr2 Laction_shr3 Laction_shr4 Laction_shr5 Laction_shr6
	gen action = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order action
	sort action
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order action
	sort action
	forvalues j = 1(1)6 ///
	{
		rename Laction_shr`j' Laction_shr`j'_sd
	}
	merge 1:1 action using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	gen sort1 = 0
	replace sort1 = 1 if action == "11"
	replace sort1 = 2 if action == "12"
	replace sort1 = 3 if action == "13"
	replace sort1 = 5 if action == "20"
	replace sort1 = 6 if action == "30"
	replace sort1 = 7 if action == "90"
	sort sort1
	drop sort1
	order action obs ind ///
	Laction_shr1 Laction_shr1_sd ///
	Laction_shr2 Laction_shr2_sd ///
	Laction_shr3 Laction_shr3_sd ///
	Laction_shr4 Laction_shr4_sd ///
	Laction_shr5 Laction_shr5_sd ///
	Laction_shr6 Laction_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_coltransit_2012rl_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear

	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	******************************
	* choices over the lifecycle, in-sample one period ahead only *
	******************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr
	*drop if tax_yr == 2012
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	*** calculate the share of action by age ***
	forvalues ag = 0(1)11 ///
	{
		preserve
		keep if age == `ag'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q]
		gen age = `ag'+`a'
		tostring(age), replace
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`ag'" == "0" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`ag'" != "0" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q]
	gen age = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	forvalues ag = 0(1)11 ///
	{
		preserve
		keep if age == `ag'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
		gen age = `ag'+`a'
		tostring(age), replace
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
	gen age = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order age
	sort age
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order age
	sort age
	forvalues j = 1(1)6 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 age using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	sort age
	order age obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd ///
	action_shr5 action_shr5_sd ///
	action_shr6 action_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_lifecycle_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	******************************
	* choices over the lifecycle, out of sample predicted *
	******************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr
	keep if tax_yr == 2012
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	*** calculate the share of action by age ***
	forvalues ag = 2(1)11 ///
	{
		preserve
		keep if age == `ag'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q]
		gen age = `ag'+`a'
		tostring(age), replace
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`ag'" == "2" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`ag'" != "2" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q]
	gen age = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	forvalues ag = 2(1)11 ///
	{
		preserve
		keep if age == `ag'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
		gen age = `ag'+`a'
		tostring(age), replace
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
	gen age = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order age
	sort age
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order age
	sort age
	forvalues j = 1(1)6 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 age using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	sort age
	order age obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd ///
	action_shr5 action_shr5_sd ///
	action_shr6 action_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_lifecycle_2012_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	******************************
	* choices over the lifecycle, out of sample real data *
	******************************
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr
	keep if tax_yr == 2012
	drop action
	rename real_action action
	
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	*** calculate the share of action by age ***
	forvalues ag = 2(1)11 ///
	{
		preserve
		keep if age == `ag'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		local datadir T:\${data_folder}\gc_esamples\
		saveold "`datadir'gc_Xp_life2012_age`ag'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
		sum action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
		local datadir T:\${data_folder}\
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int
		gen age = `ag'+`a'
		tostring(age), replace
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`ag'" == "2" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`ag'" != "2" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	local datadir T:\${data_folder}\gc_esamples\
	saveold "`datadir'gc_Xp_life2012_ageall_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
	sum action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
	local datadir T:\${data_folder}\
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int
	gen age = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	forvalues ag = 2(1)11 ///
	{
		preserve
		keep if age == `ag'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
		gen age = `ag'+`a'
		tostring(age), replace
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
	gen age = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order age
	sort age
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order age
	sort age
	forvalues j = 1(1)6 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 age using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	sort age
	order age obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd ///
	action_shr5 action_shr5_sd ///
	action_shr6 action_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_lifecycle_2012rl_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	disp "***** STEP 2: fit (COMPLETED) *****"
}
disp "***** Finished processing STEP 2 *****"
endtime

***************
* STEP 3: choices by type, data and model (plus baseline characteristics) *
***************
disp "***** Started processing STEP 3 *****"
starttime
if 1 == `gate3' ///
{
	disp "***** STEP 3: choices by type, data and model (plus baseline characteristics) *****"
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear

	* total number of years in entrepreneurship.
	sort pid_int tax_yr
	gen counter = 0
	replace counter = 1 if real_action == 30
	bys pid_int: egen tot_30 = sum(counter)
	drop counter

	* fraction of panel years in entrepreneurship
	sort pid_int tax_yr
	gen counter = 1
	bys pid_int: egen tot_yrs = sum(counter)
	gen share_30 = tot_30/tot_yrs
	drop tot_yrs counter
	
	* indicator for having at least one spell in entrepreneurship.
	gen one_30 = 0
	replace one_30 = 1 if tot_30 >=1
	
	* total number of startups *
	sort pid_int tax_yr
	gen eid_startup_non_zero = 0
	replace eid_startup_non_zero = 1 if eid_startup !=""
	bys pid_int eid_startup eid_startup_non_zero: gen counter = _n
	replace counter = 0 if counter > 1
	replace counter = 0 if eid_startup_non_zero == 0
	bys pid_int: egen N_startup = sum(counter)
	drop counter eid_startup_non_zero
	
	* indicator for having one startup *
	gen one_startup = 0
	replace one_startup =1 if N_startup >=1
	
	* indicator for having more than one startup *
	gen serial_startup = 0
	replace serial_startup =1 if N_startup >=2
	
	sort pid_int tax_yr
	keep if age == 0
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	gen city1 = 0
	replace city1 = 1 if res_city_size_entry == 1
	gen city2 = 0
	replace city2 = 1 if res_city_size_entry == 2
	gen city3 = 0
	replace city3 = 1 if res_city_size_entry == 3
	gen city4 = 0
	replace city4 = 1 if res_city_size_entry == 4
	
	rename res_pc_inc_entry inc_entry
	rename res_pc_shr_bus_entry shr_bus_entry
	rename res_pc_popden_entry popden_entry
	rename res_pc_busipopden_entry busipopden_entry
	
	foreach var in inc_entry shr_bus_entry popden_entry busipopden_entry ///
	{
		gen `var'_cat = 0
		capture sum `var' if age == 0, detail
		display "`var' 75th pctile is `r(p75)'"
		replace `var'_cat = 1 if `var' >= `r(p75)'
	}
	
	* population and business owners density per square km *
	replace popden_entry = popden_entry*1000000
	replace busipopden_entry = busipopden_entry*1000000
	
	*** calculate the mean of certain variables by type ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		local datadir T:\${data_folder}\gc_esamples\
		saveold "`datadir'gc_Xp_sstype_type`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
		sum inc_entry inc_entry_cat shr_bus_entry shr_bus_entry_cat city1 city2 city3 city4 tot_30 one_30 share_30 N_startup one_startup serial_startup
		local datadir T:\${data_folder}\
		collapse (mean) inc_entry inc_entry_cat shr_bus_entry shr_bus_entry_cat city1 city2 city3 city4 tot_30 one_30 share_30 N_startup one_startup serial_startup (count) ind=n_ind obs=pid_int [aw=q] 
		gen state = "`s'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`s'" == "1" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`s'" != "1" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	local datadir T:\${data_folder}\gc_esamples\
	saveold "`datadir'gc_Xp_sstype_typeall_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
	sum inc_entry inc_entry_cat shr_bus_entry shr_bus_entry_cat city1 city2 city3 city4 tot_30 one_30 share_30 N_startup one_startup serial_startup
	local datadir T:\${data_folder}\
	collapse (mean) inc_entry inc_entry_cat shr_bus_entry shr_bus_entry_cat city1 city2 city3 city4 tot_30 one_30 share_30 N_startup one_startup serial_startup (count) ind=n_ind obs=pid_int
	gen state = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		collapse (sd) inc_entry inc_entry_cat shr_bus_entry shr_bus_entry_cat city1 city2 city3 city4 tot_30 one_30 share_30 N_startup one_startup serial_startup [aw=q]
		gen state = "`s'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) inc_entry inc_entry_cat shr_bus_entry shr_bus_entry_cat city1 city2 city3 city4 tot_30 one_30 share_30 N_startup one_startup serial_startup
	gen state = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order state
	sort state
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order state
	sort state
	rename inc_entry inc_entry_sd
	rename inc_entry_cat inc_entry_cat_sd
	rename shr_bus_entry shr_bus_entry_sd
	rename shr_bus_entry_cat shr_bus_entry_cat_sd
	rename city1 city1_sd
	rename city2 city2_sd
	rename city3 city3_sd
	rename city4 city4_sd
	rename tot_30 tot_30_sd
	rename one_30 one_30_sd
	rename share_30 share_30_sd
	rename N_startup N_startup_sd
	rename one_startup one_startup_sd
	rename serial_startup serial_startup_sd

	merge 1:1 state using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	sort state
	order state obs ind ///
	inc_entry inc_entry_sd ///
	inc_entry_cat inc_entry_cat_sd ///
	shr_bus_entry shr_bus_entry_sd ///
	shr_bus_entry_cat shr_bus_entry_cat_sd ///
	city1 city1_sd ///
	city2 city2_sd ///
	city3 city3_sd ///
	city4 city4_sd ///
	tot_30 tot_30_sd ///
	one_30 one_30_sd ///
	share_30 share_30_sd ///
	N_startup N_startup_sd ///
	one_startup one_startup_sd ///
	serial_startup serial_startup_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_ss_types_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr
	drop if age == 0
	
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	*** calculate the share of action by type ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q]
		gen state = "`s'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`s'" == "1" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`s'" != "1" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int  [aw=q]
	gen state = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
		gen state = "`s'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
	gen state = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order state
	sort state
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order state
	sort state
	forvalues j = 1(1)6 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 state using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	sort state
	order state obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd ///
	action_shr5 action_shr5_sd ///
	action_shr6 action_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_bytype_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr
	drop if age == 0
	drop action
	rename real_action action
	
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	*** calculate the share of action by type ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		local datadir T:\${data_folder}\gc_esamples\
		saveold "`datadir'gc_Xp_bytype_type`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
		sum action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
		local datadir T:\${data_folder}\
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q]
		gen state = "`s'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`s'" == "1" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`s'" != "1" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	local datadir T:\${data_folder}\gc_esamples\
	saveold "`datadir'gc_Xp_bytype_typeall_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
	sum action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
	local datadir T:\${data_folder}\
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int  [aw=q]
	gen state = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
		gen state = "`s'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
	gen state = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order state
	sort state
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order state
	sort state
	forvalues j = 1(1)6 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 state using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	sort state
	order state obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd ///
	action_shr5 action_shr5_sd ///
	action_shr6 action_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_bytype_rl_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	forvalues ag = 1(1)11 ///
	{
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr
	
	keep if age == `ag'
	
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}

	tab action, gen(action_shr)
	*** calculate the share of action by type ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q]
		gen state = "`s'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`s'" == "1" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`s'" != "1" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int  [aw=q]
	gen state = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
		gen state = "`s'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
	gen state = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order state
	sort state
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order state
	sort state
	forvalues j = 1(1)6 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 state using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	sort state
	order state obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd ///
	action_shr5 action_shr5_sd ///
	action_shr6 action_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	local aget = `ag' + `a'
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_bytype`aget'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear
	sort pid_int tax_yr
	keep if age == `ag'
	drop action
	rename real_action action
	
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}
	
	tab action, gen(action_shr)
	*** calculate the share of action by type ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		local datadir T:\${data_folder}\gc_esamples\
		saveold "`datadir'gc_Xp_bytype`aget'_type`s'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
		sum action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
		local datadir T:\${data_folder}\
		collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int [aw=q]
		gen state = "`s'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`s'" == "1" ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		if "`s'" != "1" ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
	}
	*** calculate the mean of certain variables for all occupations ***
	preserve
	sort pid_int tax_yr
	by pid_int: gen counter_id = _n
	gen n_ind = .
	replace n_ind = 1 if counter_id == 1
	drop counter_id
	local datadir T:\${data_folder}\gc_esamples\
	saveold "`datadir'gc_Xp_bytype_type`aget'all_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
	sum action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6
	local datadir T:\${data_folder}\
	collapse (mean) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 (count) ind=n_ind obs=pid_int  [aw=q]
	gen state = "0"
	gen stat_type = "mean"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** calculate the sd of certain variables by occupation ***
	forvalues s = 1(1)`n_types' ///
	{
		preserve
		keep if state == `s'
		collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
		gen state = "`s'"
		gen stat_type = "sd"
  		* save it or stack it *
    	append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    	save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
		restore
	}
	*** calculate the sd of certain variables for all occupations ***
	preserve
	collapse (sd) action_shr1 action_shr2 action_shr3 action_shr4 action_shr5 action_shr6 [aw=q]
	gen state = "0"
	gen stat_type = "sd"
    append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
	restore
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order state
	sort state
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "sd"
	drop stat_type
	order state
	sort state
	forvalues j = 1(1)6 ///
	{
		rename action_shr`j' action_shr`j'_sd
	}
	merge 1:1 state using "`datadir'gc_reduced_tmp3_`ext'.dta", update
	drop _merge
	sort state
	order state obs ind ///
	action_shr1 action_shr1_sd ///
	action_shr2 action_shr2_sd ///
	action_shr3 action_shr3_sd ///
	action_shr4 action_shr4_sd ///
	action_shr5 action_shr5_sd ///
	action_shr6 action_shr6_sd
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_bytype`aget'_rl_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	}
	disp "***** STEP 3: fit (COMPLETED) *****"
}
disp "***** Finished processing STEP 3 *****"
endtime

***************
* STEP 5: industry breakdown by type, data *
***************
disp "***** Started processing STEP 5 *****"
starttime
if 1 == `gate5' ///
{
	disp "***** STEP 5: industry breakdown by type, data *****"
	
	local datadir T:\${data_folder}\
	use "`datadir'gc_structural_Xpost_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", clear

	sort pid_int tax_yr
	drop if age == 0
	drop action
	rename real_action action
	
	gen q = .
	forvalues s = 1(1)`n_types' ///
	{
	    replace q = q`s' if state == `s'
	}

	tab naics2_int, gen(naics_shr)
	
	*** calculate the share of action by type ***
	forvalues s = 1(1)`n_types' ///
	{
		foreach act in "11" "12" "13" ///
		{
		preserve
		keep if state == `s'
		keep if action == `act'
		sort pid_int tax_yr
		by pid_int: gen counter_id = _n
		gen n_ind = .
		replace n_ind = 1 if counter_id == 1
		drop counter_id
		local datadir T:\${data_folder}\gc_esamples\
		keep pid_int n_ind naics_shr*
		forvalues i = 1(1)18 {
			label var naics_shr`i' ""
		}
		saveold "`datadir'gc_Xp_bytype_type`s'_act`act'_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.dta", replace v(12)
		sum naics_shr1 naics_shr2 naics_shr3 naics_shr4 naics_shr5 naics_shr6 naics_shr7 naics_shr8 naics_shr9 naics_shr10 naics_shr11 naics_shr12 naics_shr13 naics_shr14 naics_shr15 naics_shr16 naics_shr17 naics_shr18
		local datadir T:\${data_folder}\
		collapse (mean) naics_shr1 naics_shr2 naics_shr3 naics_shr4 naics_shr5 naics_shr6 naics_shr7 naics_shr8 naics_shr9 naics_shr10 naics_shr11 naics_shr12 naics_shr13 naics_shr14 naics_shr15 naics_shr16 naics_shr17 naics_shr18 (count) ind=n_ind obs=pid_int
		gen state = "`s'"
		gen act = "`act'"
		gen stat_type = "mean"
  		* save it or stack it *
  		if "`s'" == "1" & "`act'" == "11"  ///
    	{
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
  		else ///
    	{
    		append using "`datadir'gc_reduced_tmp2_`ext'.dta"
    		save "`datadir'gc_reduced_tmp2_`ext'.dta", replace
    	}
		restore
		}
	}
	
	*** bring means and sds and reformat ***
	use "`datadir'gc_reduced_tmp2_`ext'.dta", clear
	keep if stat_type == "mean"
	drop stat_type
	order state act
	sort state act
	save "`datadir'gc_reduced_tmp3_`ext'.dta", replace
	
	sort state act
	order state act obs ind ///
	naics_shr1 ///
	naics_shr2 ///
	naics_shr3 ///
	naics_shr4 ///
	naics_shr5 ///
	naics_shr6 ///
	naics_shr7 ///
	naics_shr8 ///
	naics_shr9 ///
	naics_shr10 ///
	naics_shr11 ///
	naics_shr12 ///
	naics_shr13 ///
	naics_shr14 ///
	naics_shr15 ///
	naics_shr16 ///
	naics_shr17 ///
	naics_shr18 
	save "`datadir'gc_reduced_tmp4_`ext'.dta", replace
	
	* set up save table options *
	local datadir T:\${output_folder}\
	disp "`datadir'"
	export excel using "`datadir'gc_Xp_bytypeNAICS_rl_a`a'_v`v'_d`d'_q`n_types'_m`y_dgp'.xlsx", firstrow(variables) replace
	clear
	
	* delete intermediate datasets *
	local datadir T:\${data_folder}\
	capture erase "`datadir'gc_reduced_tmp2_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp3_`ext'.dta"
	capture erase "`datadir'gc_reduced_tmp4_`ext'.dta"
	}
	disp "***** STEP 3: fit (COMPLETED) *****"
}
disp "***** Finished processing STEP 3 *****"
endtime

********************************************
* STEP X: delete all intermediate datasets *
********************************************
disp "***** Started processing STEP X *****"
starttime
if 1 == `gateX' ///
{
	disp "***** STEP X: delete all intermediate datasets *****"
	local datadir T:\${data_folder}\

	disp "***** STEP X: delete all intermediate datasets (COMPLETED) *****"
}
disp "***** Finished processing STEP X *****"
endtime

********
* EXIT *
********
clear all
log close
